package hu.bme.mit.inf.seample.gen;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.mwe.core.WorkflowContextDefaultImpl;
import org.eclipse.emf.mwe.core.issues.Issues;
import org.eclipse.emf.mwe.core.issues.IssuesImpl;
import org.eclipse.emf.mwe.core.monitor.ProgressMonitorAdapter;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.xtend.check.CheckComponent;
import org.eclipse.xtend.typesystem.MetaModel;
import org.eclipse.xtend.typesystem.emf.EmfMetaModel;

import seample.SeamplePackage;

/**
 * Checks the model with XPand Check Language.
 * 
 * @author Mark Czotter
 *
 */
public class SeamApplicationSkeletonChecker {

	private final SeamGenerationDescriptor descriptor;
	
	private static final String SLOT = "seamsystem";
	private static final String CHECK_FILE = "hu::bme::mit::obp::seammodel::gen::CheckComponent";

	public SeamApplicationSkeletonChecker(SeamGenerationDescriptor descriptor) {
		this.descriptor = descriptor;
	}
	
	public Issues check(IProgressMonitor monitor) {
		CheckComponent cc = new CheckComponent();
		
		MetaModel seammm = new EmfMetaModel(SeamplePackage.eINSTANCE);
		MetaModel umlmm = new EmfMetaModel(UMLPackage.eINSTANCE);
		cc.addMetaModel(seammm);
		cc.addMetaModel(umlmm);
		
		WorkflowContextDefaultImpl ctx = new WorkflowContextDefaultImpl();
		ctx.set(SLOT, getDescriptor().getSeamSystem());
		
		cc.setEmfAllChildrenSlot(SLOT);
		
		cc.addCheckFile(CHECK_FILE);
		cc.setAbortOnError(false);
		
		Issues issues = new IssuesImpl();
		cc.preInvoke();
		cc.invoke(ctx, new ProgressMonitorAdapter(monitor), issues);
		cc.postInvoke();
		
		return issues;
	}
	
	public SeamGenerationDescriptor getDescriptor() {
		return descriptor;
	}
	
}
